package com.shopping_back.mapper;

import com.shopping_back.pojo.dto.EvaluationDTO;
import com.shopping_back.pojo.entity.Evaluation;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 商品评价表 Mapper 接口
 * </p>
 *
 * @author Jack
 * @since 2025-07-12
 */
@Mapper
public interface EvaluationMapper extends BaseMapper<Evaluation> {

    /**
     * 分页查询指定 SPU 的已审核评价
     * @param page MyBatis-Plus 分页对象
     * @param spuId 商品 SPU ID
     * @return 分页结果
     */
    IPage<com.shopping_back.pojo.vo.EvaluationVO> selectProductEvaluations(com.baomidou.mybatisplus.extension.plugins.pagination.Page<?> page,
                                                                           @org.apache.ibatis.annotations.Param("spuId") Long spuId);

    @Select("SELECT e.id, om.order_sn, oi.sku_name, e.score, e.content, e.audit_status " +
            "FROM evaluation e " +
            "JOIN order_item oi ON e.order_item_id = oi.id " +
            "JOIN order_main om ON oi.order_id = om.id " +
            "WHERE om.merchant_id = #{merchantId} " +
            "ORDER BY e.create_time DESC " +
            "LIMIT #{size} OFFSET #{offset}")
    List<EvaluationDTO> selectEvaluationsByMerchantId(Long merchantId, Integer size, Integer offset);

    @Select("SELECT COUNT(*) " +
            "FROM evaluation e " +
            "JOIN order_item oi ON e.order_item_id = oi.id " +
            "JOIN order_main om ON oi.order_id = om.id " +
            "WHERE om.merchant_id = #{merchantId}")
    long countEvaluationsByMerchantId(Long merchantId);
}
